var flag = false;
var formType;
mini.parse();

var form = new mini.Form("userForm");

function SaveData() {
    var o = form.getData();

    form.validate();
    if (form.isValid() == false) return;
    if (flag) {
        $.ajax({
            url: GatewayUrl() + "/user/add",
            type: 'post',
            contentType: "application/json",
            data: JSON.stringify(o),
            headers: {
                "Authorization": localStorage.getItem("token")
            },
            success: function (dt) {
                if (dt.code === 200) {
                    CloseWindow("save");
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
                CloseWindow();
            }
        });
    }
}


function UpdateData() {
    var o = form.getData();

    form.validate();
    if (form.isValid() == false) return;
    if (flag) {
        $.ajax({
            url: GatewayUrl() + "/user/update",
            type: 'put',
            contentType: "application/json",
            data: JSON.stringify(o),
            headers: {
                "Authorization": localStorage.getItem("token")
            },
            success: function (dt) {
                if (dt.code === 200) {
                    CloseWindow("save");
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
                CloseWindow();
            }
        });
    }
}

////////////////////
//标准方法接口定义
function SetData(data) {
    if (data.action == "edit") {
        formType = 'edit';
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        $.ajax({
            url: GatewayUrl() + "/user/getUser/" + data.uuid,
            type: 'get',
            headers: {
                "Authorization": localStorage.getItem("token")
            },
            success: function (dt) {
                if (dt.code === 200) {
                    var o = mini.decode(dt.data);
                    form.setData(o);
                }
            }
        })

    } else {
        formType = 'add';
    }
}

//自定义焦点事件
function onStatusValidation(e) {
    e.isValid = flag;
}

//监听账号的焦点事件
$("#username input").blur(function () {
    if ($(this).val() != '') {
        $.ajax({
            url: GatewayUrl() + '/user/check/' + $(this).val(),
            type: 'get',
            headers: {
                "Authorization": localStorage.getItem("token")
            },
            success: function (dt) {
                if (dt.code === 200) {
                    flag = true;
                } else {
                    flag = false;
                }
            }
        })
    }
});

function GetData() {
    var o = form.getData();
    return o;
}
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    if (formType == 'add') {
        mini.confirm("确定添加？", "确定？",
            function (action) {
                if (action == "ok") {
                    SaveData();
                }
            }
        );
    } else {
        flag = true;
        mini.confirm("确定修改？", "确定？",
            function (action) {
                if (action == "ok") {
                    UpdateData();
                }
            }
        );
    }

}
function onCancel(e) {
    CloseWindow("cancel");
}